package top.xia17.plugins.fast.user.rest;

import top.xia17.plugins.fast.common.model.Back;
import top.xia17.plugins.fast.common.model.PageData;
import top.xia17.plugins.fast.common.model.PageParam;
import top.xia17.plugins.fast.common.database.jpa.JpaUtil;
import top.xia17.plugins.fast.common.exceptions.BadRequestException;
import top.xia17.plugins.fast.common.utils.VerifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import top.xia17.plugins.fast.user.dao.entity.Role;
import top.xia17.plugins.fast.user.model.dto.RoleDto;
import top.xia17.plugins.fast.user.model.query.RoleQuery;
import top.xia17.plugins.fast.user.model.req.SetRoleHasMenusReq;
import top.xia17.plugins.fast.user.service.RoleService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 角色rest
 *
 * @author xia17
 * @date 2022/1/17
 */
@RestController
@RequestMapping("role/mgmt")
@RequiredArgsConstructor
public class RoleMgmtRest {

    private final RoleService roleService;


    /**
     * 新增角色
     *
     * @param resource /
     * @return /
     */
    @PostMapping("create")
    public Back<RoleDto> create(@RequestBody @Valid Role resource) {
        RoleDto role = roleService.createDto(resource);
        return Back.ok(role);
    }


    /**
     * 修改角色
     *
     * @param resource /
     * @return /
     */
    @PostMapping("update")
    public Back<RoleDto> update(@RequestBody @Valid Role resource) {
        RoleDto role = roleService.updateDto(resource);
        return Back.ok(role);
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return /
     */
    @PostMapping("delete")
    public Back<String> delete(@NotBlank Long id) {
        VerifyUtil.isLongId(id, "参数异常");
        roleService.deleteById(id);
        return Back.ok();
    }

    /**
     * 删除多个角色
     *
     * @param ids 角色id
     * @return /
     */
    @PostMapping("delete_by_ids")
    public Back<String> deleteByIds(@RequestBody List<Long> ids) {
        if (ids.isEmpty()){
            throw new BadRequestException("参数异常！");
        }
        roleService.deleteById(ids);
        return Back.ok();
    }


    /**
     * 角色分页列表
     *
     * @param roleQuery 查询条件
     * @param pageParam 分页参数
     * @return /
     */
    @GetMapping
    public Back<PageData<RoleDto>> page(RoleQuery roleQuery, @Valid PageParam pageParam) {
        Page<RoleDto> page = roleService.findAllDto(roleQuery, pageParam.toPageable());
        return Back.ok(JpaUtil.page(page));
    }


    /**
     * 角色列表,无分页
     *
     * @param roleQuery 查询条件
     * @return /
     */
    @GetMapping("list")
    public Back<List<RoleDto>> list(RoleQuery roleQuery) {
        List<RoleDto> list = roleService.findAllDto(roleQuery);
        return Back.ok(list);
    }

    /**
     * 获取角色拥有的菜单ids
     *
     * @param id 角色ID
     * @return /
     */
    @GetMapping("get_role_has_menu_ids/{id}")
    public Back<List<Long>> getRoleHasMenuIds(@PathVariable("id") Integer id) {
        List<Long> list = roleService.getRoleHasMenuIds(id);
        return Back.ok(list);
    }

    /**
     * 设置角色拥有的菜单
     * @param req 参数
     * @return /
     */
    @PostMapping("set_role_has_menu_ids")
    public Back<String> setRoleHasMenuIds(@RequestBody @Valid SetRoleHasMenusReq req){
        roleService.setRoleHasMenuIds(req.getRoleId(),req.getMenuIds());
        return Back.ok();
    }

}
